﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace YZDD.Controllers
{
    public class Flag
    {
        public static int flag;
    }
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            //    ViewBag.Message = "修改此模板以快速启动你的 ASP.NET MVC 应用程序。";
            if (Flag.flag == 0)
            {
                Rooms.rooms = new Room[80];
                for (int i = 0; i < 80; i++)
                {
                    Rooms.rooms[i] = new Room();
                    Rooms.rooms[i].roomid = i;
                    Rooms.rooms[i].number = i % 8 + 1;
                    Rooms.rooms[i].addnumber = 0;
                    Rooms.rooms[i].users = new User[i % 8 + 1];
                    for (int j = 0; j < i % 8 + 1; j++)
                    {
                        Rooms.rooms[i].users[j] = new User();
                        Rooms.rooms[i].users[j].state = state.FREE;
                    }
                    Rooms.rooms[i].question = new Info();
                    Rooms.rooms[i].question.qID = 1;
                }
                Flag.flag = 1;
            }

            return View();
        }

        public ActionResult About()
        {
            // ViewBag.Message = "你的应用程序说明页。";

            return View();
        }

        public ActionResult Contact()
        {
            //ViewBag.Message = "你的联系方式页。";

            return View();
        }

        public string Hall2(int roomid, string uname)
        {
            if (Rooms.rooms[roomid].addnumber == Rooms.rooms[roomid].number)
            {
                return "no";
            }
            else
            {
                for (int i = 0; i < Rooms.rooms[roomid].number; i++)
                {
                    if (Rooms.rooms[roomid].users[i].state == state.FREE)
                    {
                        Rooms.rooms[roomid].users[i].state = state.ADD;
                        Rooms.rooms[roomid].users[i].name = uname;
                        Rooms.rooms[roomid].addnumber++;
                        break;
                    }
                }
            }
            return "yes";
        }

        public ActionResult Hall(String userName, bool need, int roomid)
        {
            if (need)
            {
                for (int i = 0; i < Rooms.rooms[roomid].number; i++)
                {
                    if (Rooms.rooms[roomid].users[i].name == userName)
                    {
                        Rooms.rooms[roomid].users[i].state = state.FREE;
                        Rooms.rooms[roomid].addnumber--;
                    }
                }
            }
            ViewBag.name = userName;
            if (userName == "")
            {
                ViewBag.name = "Anonymous";
                //return RedirectToAction("Index");
            }
            ViewBag.roomid1 = 1;
            ViewBag.roomid1addnumber = Rooms.rooms[1].addnumber;
            ViewBag.roomid1number = Rooms.rooms[1].number;

            ViewBag.roomid2 = 2;
            ViewBag.roomid2addnumber = Rooms.rooms[2].addnumber;
            ViewBag.roomid2number = Rooms.rooms[2].number;

            ViewBag.roomid3 = 3;
            ViewBag.roomid3addnumber = Rooms.rooms[3].addnumber;
            ViewBag.roomid3number = Rooms.rooms[3].number;

            ViewBag.roomid4 = 4;
            ViewBag.roomid4addnumber = Rooms.rooms[4].addnumber;
            ViewBag.roomid4number = Rooms.rooms[4].number;

            ViewBag.roomid5 = 5;
            ViewBag.roomid5addnumber = Rooms.rooms[5].addnumber;
            ViewBag.roomid5number = Rooms.rooms[5].number;

            ViewBag.roomid6 = 6;
            ViewBag.roomid6addnumber = Rooms.rooms[6].addnumber;
            ViewBag.roomid6number = Rooms.rooms[6].number;

            ViewBag.roomid7 = 7;
            ViewBag.roomid7addnumber = Rooms.rooms[7].addnumber;
            ViewBag.roomid7number = Rooms.rooms[7].number;

            return View();
        }

        public ActionResult Room(String uname, int roomid)
        {
            ViewBag.name = uname;
            ViewBag.roomid = roomid;
     /*       Rooms.rooms[roomid].addnumber++;

            int i = 0;
            while (true)
            {
                if (Rooms.rooms[roomid].users[i].state == state.FREE)
                {
                    Rooms.rooms[roomid].users[i].name = uname;
                    Rooms.rooms[roomid].users[i].state = state.ADD;
                    break;
                }
                i++;
            }
      */
            return View();
        }
    }
}
